package com.woniuxy.subscribe;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.woniuxy.commen.ConnUtils;
import com.woniuxy.commen.Constansts;

import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;

public class SubPublisher {
    public static void main(String[] args) throws IOException, TimeoutException {
        //建立连接

        //建立信道
        Channel channel = ConnUtils.getconn();

        //(1)创建交换机,类型是FANOUT
        channel.exchangeDeclare(Constansts.SUB_EXCHANGE, BuiltinExchangeType.FANOUT);

        //(2)取消创建消息队列

        //发送消息
        /*
         exchange 交换机
         routing-key 路由键
         properties 属性
         body 消息体
         */
        Scanner scanner = new Scanner(System.in);
        while (true){
            String message = scanner.next();
            if ("exit".equals(message)){
                break;
            }
            //(3) 发给交换机，不提供routing-key
            channel.basicPublish(Constansts.SUB_EXCHANGE,"",null,message.getBytes("UTF-8"));
            System.out.println("[x] 消息发送成功！ "+message);
        }

    }
}
